package com.new2i.service.impl;
  
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
import com.new2i.dao.able.DoctorUserDao;
import com.new2i.dao.able.DoctorUserGroupDao;
import com.new2i.dao.able.EmrNeonatusDao;
import com.new2i.dao.able.UserDao; 
import com.new2i.model.DoctorEntity;
import com.new2i.model.DoctorUserEntity;
import com.new2i.model.UserEntity; 
import com.new2i.service.able.DoctorUserService;
import com.new2i.util.EncryptionUtil; 
import com.weixin.util.WeixinHttpUtil;

/**
 * 医生管理用户的业务逻辑处理接口
 * 
 * @since 2015/09/03
 * @author 2i
 *
 */
@Service("DoctorUserService")
public class DoctorUserServiceImpl implements DoctorUserService {
 
	@Autowired
	private DoctorUserDao mDoctorUserDao;

	@Autowired
	private UserDao mUserDao; 
 
	
	@Autowired
	private DoctorUserGroupDao mDoctorUserGroupDao;
	 
	@Autowired
	private EmrNeonatusDao mEmrNeonatusDao;
	
	private static Logger logger = Logger.getLogger(DoctorUserServiceImpl.class);
	
	@Override
	public HashMap<String, Object> getUserListAndCount(DoctorUserEntity doctorUser) {
		HashMap<String, Object> hashMap = new HashMap<String, Object>(); 
		int doctor_id = doctorUser.getDoctor_id();
		int du_group_id = doctorUser.getDu_group_id();
		String keyword = doctorUser.getKeyword();
		int page = doctorUser.getPage();
		if (doctor_id == 0) {
			return hashMap;
		}
		if (keyword == null)
			keyword = "";
		if (page < 1)
			page = 1; 
		List<HashMap<String, Object>> userList = mDoctorUserDao.selectUserList(doctor_id, keyword, du_group_id, page); 
		List<HashMap<String, Object>> groupList = mDoctorUserGroupDao.selectGroupList(doctor_id);
		HashMap<String, Object> userCountMap = mDoctorUserDao.getUserCount(doctor_id, keyword, du_group_id);
		HashMap<String, Object> totalUserCountMap = mDoctorUserDao.getTotalUserCount(doctor_id);   
		//获取未分组用户的数目
		HashMap<String, Object> unClassifyGroupMap = mDoctorUserDao.getUserCount(doctor_id, "", -1); 
		unClassifyGroupMap.put("du_group_id", -1);
		unClassifyGroupMap.put("du_group_name", "未分组");
		//将其加入分组列表信息中 
		groupList.add(unClassifyGroupMap);   
		hashMap.put("user_list", userList);
		hashMap.put("group_list", groupList); 
		hashMap.putAll(userCountMap);
		hashMap.putAll(totalUserCountMap);
		logger.debug("contactList length"+userList.size()); 
		return hashMap;
	}

	@Override
	public HashMap<String, Object> insertDoctorUser(DoctorEntity doctor, UserEntity user) {
		HashMap<String, Object> sHashMap = new HashMap<String, Object>();
		try {  
			Date now = new Date(); 
			String token = EncryptionUtil.getTimestampToken();   
			user.setUser_photourl("http://shiqi-2i-user.qiniudn.com/neonatus.png");
			//泉州公众号 
			user.setPublic_number_source(WeixinHttpUtil.WX_PLATFROM_NUM);
			user.setSource(1);//微信用户的关联人 
			user.setCreate_date(now);
			user.setUpdate_date(now);
			user.setExist_status(0);
			user.setUser_type(2);//新生儿用户
			user.setUser_token(token);
			user.setPassword("123456");
			user.setPlatform(4);
			UserEntity rUser = mUserDao.insertUser(user);
			if(rUser.getUser_id() == 0) {
				return null;  
			} 
			//更新病历号
			mUserDao.updateUserEmrNoByUserId(rUser.getUser_id()); 
//			// 保存新生儿出生信息 
//			userNeo.setUser_id(rUser.getUser_id()); 
//			userNeo.setExist_status(0);
//			userNeo.setCreate_date(now);
//			userNeo.setUpdate_date(now);
//			mUserNeonatusDao.insertUserNeonatus(userNeo);
			 
			DoctorUserEntity doctorUser = new DoctorUserEntity();
			doctorUser.setUser_id(rUser.getUser_id());  
			doctorUser.setDoctor_id(doctor.getDoctor_id()); 
			doctorUser.setDoctor_user_uuid(token);
			doctorUser.setExist_status(0);
			doctorUser.setCreate_date(now);
			doctorUser.setUpdate_date(now);
			doctorUser.setComplete_status(0);
			doctorUser.setDu_group_id(-1);
			doctorUser.setShiqi_status(0);
			doctorUser.setUser_status(1); 
			DoctorUserEntity rDoctorUser = mDoctorUserDao.insertDoctorUser(doctorUser);
			sHashMap.put("doctor_user_id", rDoctorUser.getDoctor_user_id());
			sHashMap.put("user_id", rUser.getUser_id());
		}catch (Exception e) { 
			logger.error(e.getLocalizedMessage());
			return null; 
		} 
		return sHashMap; 
	}

	@Override
	public HashMap<String, Object> getUserInfoAndEmrList(DoctorUserEntity doctorUser) {
		HashMap<String, Object> rHashMap = new HashMap<String, Object>();
		int user_id = doctorUser.getUser_id();
		int doctor_id = doctorUser.getDoctor_id();
		HashMap<String, Object> userInfo = mUserDao.getUserInfoById(user_id); 
		List<HashMap<String, Object>> emrList = mEmrNeonatusDao.selectEmrListByUserId(user_id, doctor_id);
		if(emrList == null)
			emrList = new ArrayList<HashMap<String, Object>>();
		for (HashMap<String, Object> sHashMap : emrList) {
			List<HashMap<String, Object>> osUrlList = new ArrayList<HashMap<String, Object>>();
			List<HashMap<String, Object>> odUrlList = new ArrayList<HashMap<String, Object>>(); 
			// 病历图片json array
			String urlStrs = (String) sHashMap.get("emrpic_urls");
			if (urlStrs != null) {
				String contents[] = urlStrs.split("~");
				for (String content : contents) {
					// 病历图片json info
					if (content.startsWith(",")) {
						content = content.substring(1, content.length());
					}
					String values[] = content.split(",");
					HashMap<String, Object> pic_map = new HashMap<String, Object>();
					int emr_pic_id = Integer.valueOf(values[0]);
					int index_type = Integer.valueOf(values[1]);
					int source = Integer.valueOf(values[2]);
					int emrpic_type = Integer.valueOf(values[3]);
					String emrpic_url = values.length > 4 ? values[4] : "";
					String pic_desc = values.length > 5 ? values[5] : "";
					pic_map.put("emr_pic_id", emr_pic_id);
					pic_map.put("index_type", index_type);
					pic_map.put("source", source);
					pic_map.put("pic_type", emrpic_type);
					pic_map.put("pic_url", emrpic_url);
					pic_map.put("pic_desc", pic_desc); 
					if (index_type == 1)
						osUrlList.add(pic_map);
					else
						odUrlList.add(pic_map);
				}
			}
			sHashMap.put("od_urls", odUrlList);
			sHashMap.put("os_urls", osUrlList); 
			sHashMap.remove("emrpic_urls");
		}     
		rHashMap.put("emr_list", emrList);  
		rHashMap.put("user_info", userInfo); 
		return rHashMap;
	} 
}
